package com.yulongtian.handler;


import com.alibaba.fastjson.JSON;
import com.yulongtian.utils.R;
import com.yulongtian.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拒绝访问权限不足
 * @author yulongTian
 * @create 2022-10-30 21:12
 */
@Component
public class AccessDeniedExceptionImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        //未授权
//        R responseResult = new R(HttpStatus.FORBIDDEN.value(), "您的权限不足");
        String jsonString = JSON.toJSONString(R.error().data("data","您的权限不足"));
        //自定义处理异常
        WebUtils.renderString(response, jsonString);
    }
}
